package com.persondemo.videoappliction.ui.dianping;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.DianPingDetilsBean;
import com.persondemo.videoappliction.bean.ShopDetilsBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;
import com.persondemo.videoappliction.ui.adapter.DPdetilsAdapter;
import com.persondemo.videoappliction.ui.base.AppManager;
import com.persondemo.videoappliction.ui.base.BaseActivity;
import com.persondemo.videoappliction.ui.dianping.contract.DPdetilsContract;
import com.persondemo.videoappliction.ui.dianping.presenter.DPdetilsPresenter;
import com.persondemo.videoappliction.utils.T;
import com.persondemo.videoappliction.widget.CustomLoadMoreView;
import com.persondemo.videoappliction.widget.SimpleMultiStateView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public class PinglunActivity extends BaseActivity<DPdetilsPresenter> implements DPdetilsContract.View {
    @BindView(R.id.tou_relate)
    RelativeLayout touRelate;
    @BindView(R.id.item_dianping_pinglun_Ratingbar)
    RatingBar itemDianpingPinglunRatingbar;
    @BindView(R.id.pinglun_btn01)
    Button pinglunBtn01;
    @BindView(R.id.pinglun_btn02)
    Button pinglunBtn02;
    @BindView(R.id.pinglun_btn03)
    Button pinglunBtn03;
    @BindView(R.id.pinglun_btn04)
    Button pinglunBtn04;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.SimpleMultiStateView)
    com.persondemo.videoappliction.widget.SimpleMultiStateView SimpleMultiStateView;
    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    @BindView(R.id.pinglun_bakc)
    ImageView pinglunBakc;
    private int page = 1;//页数
    private String id;
    private DPdetilsAdapter adapter;
    private List<DianPingDetilsBean.DataBean.ListBean> list = null;
    private int haoping = 0, youtu = 0, chaping = 0;
    private String type = "";
    private View footview;
    private TextView foot_text;
    @Override
    public void onRetry() {

    }

    @Override
    public int getContentLayout() {
        return R.layout.module_activity_pinglun;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        pinglunBakc.setOnClickListener(v -> AppManager.getAppManager().finishActivity());
        footview = getLayoutInflater().inflate(R.layout.dianpingdetils_foot, null);
        foot_text = footview.findViewById(R.id.foot_text);
        foot_text.setText("没有评论了，点击前往写评论！");
        foot_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(PinglunActivity.this, PutDianPingActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("id",id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        mPtrFrameLayout.disableWhenHorizontalMove(true);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                type = "";
                if (mPresenter != null) {
                    mPresenter.getdainping(id, page + "", type);
                }
            }
        });
        mRecyclerView.setLayoutManager(new GridLayoutManager(PinglunActivity.this, 1));
        adapter = new DPdetilsAdapter(list, PinglunActivity.this);
        mRecyclerView.setAdapter(adapter);
        adapter.setEnableLoadMore(true);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
        mRecyclerView.setAdapter(adapter);
        adapter.setEnableLoadMore(true);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (mPresenter != null) {
                    mPresenter.getdainping(id, page + "", type);
                }
            }
        }, mRecyclerView);
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        if (intent!=null){
            id = intent.getStringExtra("id");
            Log.i("id", "id=" + id);
        }

        list = new ArrayList<>();
        if (mPresenter != null) {
            showLoadingDialog();
            mPresenter.getshop(id);
            mPresenter.getdainping(id, page + "", type);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.pinglun_btn01, R.id.pinglun_btn02, R.id.pinglun_btn03, R.id.pinglun_btn04})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pinglun_btn01:
                page = 1;
                type = "";
                if (mPresenter != null) {
                    showLoadingDialog();
                    list.clear();
                    adapter.notifyDataSetChanged();
                    mPresenter.getdainping(id, page + "", type);
                }
                break;
            case R.id.pinglun_btn02:
                type = "3";
                page = 1;
                if (youtu == 0) {
                    T.showShort(PinglunActivity.this, "暂时没有有图评价!");
                    return;
                }
                if (mPresenter != null) {
                    showLoadingDialog();
                    list.clear();
                    adapter.notifyDataSetChanged();
                    mPresenter.getdainping(id, page + "", type);
                }
                break;
            case R.id.pinglun_btn03:
                type = "1";
                page = 1;
                if (haoping == 0) {
                    T.showShort(PinglunActivity.this, "暂时没有好评!");
                    return;
                }
                if (mPresenter != null) {
                    showLoadingDialog();
                    list.clear();
                    adapter.notifyDataSetChanged();
                    mPresenter.getdainping(id, page + "", type);
                }
                break;
            case R.id.pinglun_btn04:
                type = "2";
                page = 1;
                if (chaping == 0) {
                    T.showShort(PinglunActivity.this, "暂时没有差评!");
                    return;
                }
                if (mPresenter != null) {
                    list.clear();
                    adapter.notifyDataSetChanged();
                    showLoadingDialog();
                    mPresenter.getdainping(id, page + "", type);
                }
                break;
        }
    }

    @Override
    public void loadshop(ShopDetilsBean shopDetilsBean) {

    }

    @Override
    public void loaddianping(DianPingDetilsBean dianPingDetilsBean) {
        hideLoadingDialog();
        if (dianPingDetilsBean.getStatus() == 1) {
            if (dianPingDetilsBean.getData().getTotal() != 0) {
                pinglunBtn01.setText("最新评价(" + dianPingDetilsBean.getData().getTotal() + "）");
            }
            youtu = dianPingDetilsBean.getData().getYpnum();
            if (dianPingDetilsBean.getData().getYpnum() != 0) {
                pinglunBtn02.setText("有图(" + dianPingDetilsBean.getData().getYpnum() + ")");
            }
            haoping = dianPingDetilsBean.getData().getHpnum();
            if (dianPingDetilsBean.getData().getHpnum() != 0) {
                pinglunBtn03.setText("好评(" + dianPingDetilsBean.getData().getHpnum() + ")");
            }
            chaping = dianPingDetilsBean.getData().getCpnum();
            if (dianPingDetilsBean.getData().getCpnum() != 0) {
                pinglunBtn04.setText("(差评(" + dianPingDetilsBean.getData().getCpnum() + ")");
            }

            if (dianPingDetilsBean.getData().getList() == null || dianPingDetilsBean.getData().getList().size() == 0) {
                hideLoadingDialog();
                T.showShort(PinglunActivity.this, "没有更多数据了！");
                list.clear();
                adapter.setNewData(null);
                adapter.removeAllFooterView();
                adapter.addFooterView(footview);
                return;
            }
            if (dianPingDetilsBean.getData().getList() != null && dianPingDetilsBean.getData().getList().size() > 0) {
                list.clear();
                adapter.removeAllFooterView();
                list = dianPingDetilsBean.getData().getList();
                adapter.setNewData(list);
                mPtrFrameLayout.refreshComplete();
                showSuccess();
                page++;
                if (list.size() < 10) {
                    adapter.loadMoreEnd();
                    adapter.addFooterView(footview);
                }
            }
        } else {
            T.showShort(PinglunActivity.this, dianPingDetilsBean.getMsg());
        }
    }

    @Override
    public void loadmore(DianPingDetilsBean dianPingDetilsBean) {
        if (dianPingDetilsBean.getData().getList() == null || dianPingDetilsBean.getData().getList().size() == 0) {
            adapter.loadMoreEnd();
            adapter.addFooterView(footview);
            T.showShort(PinglunActivity.this, "没有更多数据了！");
            return;
        } else {
            page++;
            adapter.addData(dianPingDetilsBean.getData().getList());
            adapter.loadMoreComplete();
        }
    }
}
